//
//  QuizView.m
//  DriftDog
//
//  Created by rimi on 15/9/7.
//  Copyright (c) 2015年 廖马林. All rights reserved.
//

#import "QuizView.h"

static NSString *const kanimationDog = @"kanimationDog";

@interface QuizView ()

//@property (nonatomic, strong) CAGradientLayer *gradientLayer;
@property (nonatomic, strong) CALayer *dog;
@property (nonatomic, strong) UIImageView *leftCloud;
@property (nonatomic, strong) UIImageView *rightCloud;
@property (nonatomic, strong) UITextField *userNameTextField;
@property (nonatomic, strong) UITextField *passwordTextField;
@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, strong) UIButton *forgetPasswordButtton;
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UILabel *otherwaysToLogin;
@property (nonatomic, strong) UIButton *qqButton;
@property (nonatomic, strong) UIButton *weChatButton;
@property (nonatomic, strong) CAShapeLayer *layerOne;
@property (nonatomic, strong) CAShapeLayer *layerTwo;
@property (nonatomic, strong) CAShapeLayer *layerThree;

@end

@implementation QuizView

- (instancetype)init
{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        [self initializeAppearance];
    }
    return self;
}

- (void)initializeAppearance {

    self.backgroundColor = [UIColor colorWithRed:180.f/255 green:217.f/255 blue:255.f/255 alpha:1];

    [self addSubview:self.leftCloud];
    [self addSubview:self.rightCloud];
    [self.layer addSublayer:self.dog];
    [self addSubview:self.userNameTextField];
    [self addSubview:self.passwordTextField];
    [self addSubview:self.registerButton];
    [self addSubview:self.forgetPasswordButtton];
    [self addSubview:self.loginButton];
    [self addSubview:self.otherwaysToLogin];
    [self addSubview:self.qqButton];
    [self addSubview:self.weChatButton];
    [self.layer addSublayer:self.layerOne];
    [self.layer addSublayer:self.layerTwo];
    [self.layer addSublayer:self.layerThree];
    
    [self animationDog];
    [self animationWithLeftCloud];
    [self animationWithRightCloud];
}

#pragma mark
#pragma mark - private method
- (void)animationDog {
    
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"position.y";
    animation.duration = 1.4;
    animation.fromValue = @(self.dog.position.y);
    animation.toValue = @(185 * DHFlexibleVerticalMutiplier());

    animation.repeatCount = HUGE_VAL;
    animation.autoreverses = YES;
    [self.dog addAnimation:animation forKey:@"kanimationDog"];
}
- (void)animationWithLeftCloud {

    [UIView animateWithDuration:8.0 animations:^{
       
        self.leftCloud.frame = CGRectOffset(self.leftCloud.frame, ORIGIN_WIDTH + 550, 0);
        
    } completion:^(BOOL finished) {
        
        [self.leftCloud setFrame:DHFlexibleFrame(CGRectMake(-500, 92, 100, 100), YES)];
        [self animationWithLeftCloud];
    }];
}

- (void)animationWithRightCloud {

    [UIView animateWithDuration:5.0 animations:^{
        
        self.rightCloud.frame = CGRectOffset(self.rightCloud.frame, ORIGIN_WIDTH + 250, 0);
    } completion:^(BOOL finished) {
       
        [self.rightCloud setFrame:DHFlexibleFrame(CGRectMake(-200, 142, 150, 150), YES)];
        [self animationWithRightCloud];
    }];
}
#pragma mark
#pragma mark - getter
- (CALayer *)dog {
    
    if (!_dog) {
        _dog = ({
            
            CALayer *layer = [CALayer layer];
        
            layer.bounds = DHFlexibleFrame(CGRectMake(0, 0, 83, 175), YES);
            layer.position = DHFlexibleCenter(CGPointMake(ORIGIN_WIDTH / 2, 135));
            layer.contents = (__bridge id)[UIImage imageNamed:@"礼物小狗"].CGImage;
            layer;
        });
    }
    return _dog;
}
- (UIImageView *)leftCloud {

    if (!_leftCloud) {
        _leftCloud = ({
        
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(-500, 92, 100, 100), YES)];
            imageView.image = [UIImage imageNamed:@"白云"];
            
            imageView;
        });
        
    }
    return _leftCloud;
}
- (UIImageView *)rightCloud {

    if (!_rightCloud) {
        _rightCloud = ({
        
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(-200, 142, 150, 150), YES)];
            imageView.image = [UIImage imageNamed:@"白云"];
            
            imageView;

        });
    }
    return _rightCloud;
}

- (UITextField *)userNameTextField {

    if (!_userNameTextField) {
        _userNameTextField = ({
        
            UITextField *textField = [[UITextField alloc] initWithFrame:DHFlexibleFrame(CGRectMake(ORIGIN_WIDTH / 2 - 137, ORIGIN_HEIGHT / 2 - 90, 275, 40), YES)];
            textField.placeholder = @"   请输入用户名";
            textField.backgroundColor = [UIColor whiteColor];
            [textField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
            [textField setValue:[UIFont boldSystemFontOfSize:16 * DHFlexibleVerticalMutiplier()]forKeyPath:@"_placeholderLabel.font"];
            textField.layer.cornerRadius = 20 * DHFlexibleVerticalMutiplier();
            textField.layer.borderWidth = 1;
            textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"user.png"]];
             textField.tintColor = [UIColor lightGrayColor];
            textField.leftView = imageView;
            textField.leftViewMode = UITextFieldViewModeAlways;
            textField.layer.sublayerTransform = CATransform3DMakeTranslation(10.0f, 0.f, 0.f);
            textField;
        });
    }
    return _userNameTextField;
}

- (UITextField *)passwordTextField {

    if (!_passwordTextField) {
        _passwordTextField = ({
        
            UITextField *textField = [[UITextField alloc] initWithFrame:DHFlexibleFrame(CGRectMake(ORIGIN_WIDTH / 2 - 137, ORIGIN_HEIGHT / 2 - 20, 275, 40), YES)];
            textField.placeholder = @"  请输入用密码";
            textField.backgroundColor = [UIColor whiteColor];
            [textField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
            [textField setValue:[UIFont boldSystemFontOfSize:16 * DHFlexibleVerticalMutiplier()]forKeyPath:@"_placeholderLabel.font"];

            textField.layer.cornerRadius = 20 * DHFlexibleVerticalMutiplier();
            textField.layer.borderWidth = 1;
            textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lock.png"]];
            textField.tintColor = [UIColor lightGrayColor];
            
            textField.leftView = imageView;
            textField.leftViewMode = UITextFieldViewModeAlways;
            textField.layer.sublayerTransform = CATransform3DMakeTranslation(15.0f, 0.f, 0.f);
            textField;

        });
    }
    return _passwordTextField;
}
- (UIButton *)registerButton {

    if (!_registerButton) {
        _registerButton = ({
        
            UIButton *button = [[UIButton alloc] initWithFrame:DHFlexibleFrame(CGRectMake(ORIGIN_WIDTH / 2 - 140, ORIGIN_HEIGHT / 2 + 45, 100, 20), YES)];
            [button setTitle:@"注册新用户" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            
            button;
        });
    }
    return _registerButton;
}
- (UIButton *)forgetPasswordButtton {

    if (!_forgetPasswordButtton) {
        _forgetPasswordButtton = ({
        
            UIButton *button = [[UIButton alloc] initWithFrame:DHFlexibleFrame(CGRectMake(ORIGIN_WIDTH / 2 + 30, ORIGIN_HEIGHT / 2 + 45, 100, 20), YES)];
            [button setTitle:@"忘记密码" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            
            button;

        });
    }
    return _forgetPasswordButtton;
}
- (UIButton *)loginButton {

    if (!_loginButton) {
        _loginButton = ({
        
            UIButton *button = [[UIButton alloc] initWithFrame:DHFlexibleFrame(CGRectMake(ORIGIN_WIDTH / 2 - 67.5, ORIGIN_HEIGHT / 2 + 125, 135, 45), YES)];
            [button setTitle:@"登陆" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.backgroundColor = [UIColor brownColor];
            button.layer.cornerRadius = 22.5 * DHFlexibleVerticalMutiplier();
            button;

        });
    }
    return _loginButton;
}
- (UILabel *)otherwaysToLogin {

    if (!_otherwaysToLogin) {
        _otherwaysToLogin = ({
        
            UILabel *label = [[UILabel alloc] initWithFrame:DHFlexibleFrame(CGRectMake(ORIGIN_WIDTH / 2 - 70, ORIGIN_HEIGHT / 2 + 230, 140, 30), YES)];
            label.text = @"或用以下方式登录";
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor whiteColor];
            label.font = [UIFont boldSystemFontOfSize:15];
            label;
        });
    }
    return _otherwaysToLogin;
}
- (UIButton *)qqButton {

    if (!_qqButton) {
        _qqButton = ({
        
            UIButton *button = [[UIButton alloc] initWithFrame:DHFlexibleFrame(CGRectMake(ORIGIN_WIDTH / 2 - 150, ORIGIN_HEIGHT / 2 + 270, 70, 70), YES)];
            [button setImage:[UIImage imageNamed:@"QQ图标"] forState:UIControlStateNormal];
            button.layer.cornerRadius = 35;
            button;
        });
    }
    return _qqButton;
}
- (UIButton *)weChatButton {

    if (!_weChatButton) {
        _weChatButton = ({
        
            UIButton *button = [[UIButton alloc] initWithFrame:DHFlexibleFrame(CGRectMake(ORIGIN_WIDTH / 2 + 80, ORIGIN_HEIGHT / 2 + 270, 70, 70), YES)];
            [button setImage:[UIImage imageNamed:@"wechat"] forState:UIControlStateNormal];
            button.layer.cornerRadius = 35;
            
            button;

        });
    }
    return _weChatButton;
}
- (CAShapeLayer *)layerOne {

    if (!_layerOne) {
        _layerOne = ({
        
            CAShapeLayer *layer = [CAShapeLayer layer];
            layer.frame = DHFlexibleFrame(CGRectMake(0, 0, 4, 30), YES);
            layer.position = DHFlexibleCenter(CGPointMake(ORIGIN_WIDTH / 2, ORIGIN_HEIGHT / 2 + 60));
            layer.fillColor = [UIColor clearColor].CGColor;
            layer.lineWidth = 1;
            layer.strokeColor = [UIColor brownColor].CGColor;
            UIBezierPath *path = [UIBezierPath bezierPathWithRect:DHFlexibleFrame(CGRectMake(0, 0, 1, 30), YES)];
            layer.path = path.CGPath;
            
            layer;
        });
    }
    return _layerOne;
}
- (CAShapeLayer *)layerTwo {

    if (!_layerTwo) {
        _layerTwo = ({
        
            CAShapeLayer *layer = [CAShapeLayer layer];
            layer.frame = DHFlexibleFrame(CGRectMake(0, 0, 100, 4), YES);
            layer.position = DHFlexibleCenter(CGPointMake(ORIGIN_WIDTH / 2 - 120, ORIGIN_HEIGHT / 2 + 245));
            layer.fillColor = [UIColor clearColor].CGColor;
            layer.lineWidth = 1;
            layer.strokeColor = [UIColor whiteColor].CGColor;
            UIBezierPath *path = [UIBezierPath bezierPathWithRect:DHFlexibleFrame(CGRectMake(0, 0, 100, 1), YES)];
            layer.path = path.CGPath;
            
            layer;

        });
    }
    return _layerTwo;
}
- (CAShapeLayer *)layerThree {

    if (!_layerThree) {
        _layerThree = ({
        
            CAShapeLayer *layer = [CAShapeLayer layer];
            layer.frame = DHFlexibleFrame(CGRectMake(0, 0, 100, 4), YES);
            layer.position = DHFlexibleCenter(CGPointMake(ORIGIN_WIDTH / 2 + 120, ORIGIN_HEIGHT / 2 + 245));
            layer.fillColor = [UIColor clearColor].CGColor;
            layer.lineWidth = 1;
            layer.strokeColor = [UIColor whiteColor].CGColor;
            UIBezierPath *path = [UIBezierPath bezierPathWithRect:DHFlexibleFrame(CGRectMake(0, 0, 100, 1), YES)];
            layer.path = path.CGPath;
            
            layer;

        });
    }
    return _layerThree;
}
//回收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [self endEditing:YES];
}
@end
